package com.xm.dao.background;

import com.xm.entity.LogisticsCompanies;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 物流公司
 * @author 康文程
 * @date 2024-1-11
 */
public class LogisticsCompaniesDao extends BaseUtil {
    /**
     * 查询全部物流公司
     *
     * @return 物流公司列表
     * @throws SqlErrorException SQL异常
     */
    public List<LogisticsCompanies> getAllLogisticsCompanies() throws SqlErrorException {
        @Language("sql")
        String sql = "SELECT * FROM logistics_companies";
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(sql);
            List<LogisticsCompanies> logisticsCompaniesList = new ArrayList<>();
            while (resultSet.next()) {
                LogisticsCompanies logisticsCompanies = new LogisticsCompanies();
                logisticsCompanies.setLogisticsCompaniesId(resultSet.getInt("logistics_companies_id"));
                logisticsCompanies.setLogisticsCompaniesName(resultSet.getString("logistics_companies_name"));
                logisticsCompanies.setLogisticsCompaniesHead(resultSet.getString("logistics_companies_head"));
                logisticsCompanies.setLogisticsCompaniesPhone(resultSet.getString("logistics_companies_phone"));
                logisticsCompanies.setLogisticsCompaniesMailbox(resultSet.getString("logistics_companies_mailbox"));
                logisticsCompaniesList.add(logisticsCompanies);
            }
            return logisticsCompaniesList;
        } catch (SQLException e) {
            throw new SqlErrorException("查询全部物流公司失败", e);
        } finally {
            close(resultSet);
        }
    }
    /**
     * 更新指定ID的物流公司信息
     *
     * @param logisticsCompanies 要更新的物流公司信息
     * @throws SqlErrorException SQL异常
     */
    public void updateLogisticsCompanies(LogisticsCompanies logisticsCompanies) throws SqlErrorException {
        @Language("SQL")
        String sql = "UPDATE logistics_companies SET " +
                "logistics_companies_name = ?, " +
                "logistics_companies_head = ?, " +
                "logistics_companies_phone = ?, " +
                "logistics_companies_mailbox = ? " +
                "WHERE logistics_companies_id = ?";
        Object[] params = {
                logisticsCompanies.getLogisticsCompaniesName(),
                logisticsCompanies.getLogisticsCompaniesHead(),
                logisticsCompanies.getLogisticsCompaniesPhone(),
                logisticsCompanies.getLogisticsCompaniesMailbox(),
                logisticsCompanies.getLogisticsCompaniesId()
        };
        executeUpdate(sql, params);
    }

    /**
     * 根据物流公司ID更新物流公司信息
     *
     * @param logisticsCompanies 更新后的物流公司信息
     * @throws SqlErrorException SQL异常
     */
    public void updateLogisticsCompaniesById(LogisticsCompanies logisticsCompanies) throws SqlErrorException {
        updateLogisticsCompanies(logisticsCompanies);
    }
    /**
     * 新增物流公司
     *
     * @param logisticsCompanies 要新增的物流公司信息
     * @throws SqlErrorException SQL异常
     */
    public void addLogisticsCompanies(LogisticsCompanies logisticsCompanies) throws SqlErrorException {
        @Language("SQL")
        String sql = "INSERT INTO logistics_companies (logistics_companies_name, logistics_companies_head, " +
                "logistics_companies_phone, logistics_companies_mailbox) VALUES (?, ?, ?, ?)";
        Object[] params = {
                logisticsCompanies.getLogisticsCompaniesName(),
                logisticsCompanies.getLogisticsCompaniesHead(),
                logisticsCompanies.getLogisticsCompaniesPhone(),
                logisticsCompanies.getLogisticsCompaniesMailbox()
        };
        try {
            executeUpdate(sql, params);
        } catch (SQLException e) {
            throw new SqlErrorException("新增物流公司失败", e);
        }
    }
}
